package com.phospherus.customerinfo.service.impl;

import com.phospherus.common.service.IAttachedFileService;
import com.phospherus.common.service.impl.BaseServiceImpl;
import com.phospherus.customerinfo.entity.Customer;
import com.phospherus.customerinfo.repostory.CustomerRepository;
import com.phospherus.customerinfo.service.ICustomerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

/**
 * @author songjinlong
 * @version 1.0
 * @since 2017/12/18
 */
@Service
@Transactional
public class CustomerServiceImpl extends BaseServiceImpl<Customer, Long, CustomerRepository> implements ICustomerService {
    @Autowired
    private IAttachedFileService attachedFileService;

    @Override
    public Customer save(Customer entity) {
        super.save(entity);
        String fileList = entity.getFileList();
        if (StringUtils.isNotBlank(fileList)) {
            attachedFileService.addCustomerAttachedFile(fileList, entity.getId());
        }
        return entity;
    }

    @Override
    public String check(Customer customer) {
        if(customer.getId()!=null && customer.getId()>-1){
            return null;
        }
        String message = "";
        Customer c = repository.findByCustomerCode(customer.getCustomerCode());
        if (c != null) {
            message += "编码已存在！";
        }
        c = repository.findByCustomerName(customer.getCustomerName());
        if (c != null) {
            message += "公司名称已存在！";
        }
        c = repository.findByCustomerAbbreviation(customer.getCustomerAbbreviation());
        if (c != null) {
            message += "公司简称已存在！";
        }
        c = repository.findByCustomerAddress(customer.getCustomerAddress());
        if (c != null) {
            message += "地址已存在！";
        }

        return message;
    }
}
